package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.service.AdminLiquorLibraryService;
import org.linlinjava.litemall.admin.service.AdminLiquorTopicService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.domain.IliquormallTopic;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@RequestMapping("/admin/liquor/library")
@Validated
public class AdminLiquorLibraryController {
    private final Log logger = LogFactory.getLog(AdminLiquorLibraryController.class);

    @Autowired
    private AdminLiquorLibraryService adminLiquorLibraryService;

    /**
     * 添加酒库
     *
     * @param liquorLibrary
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:create")
    @PostMapping("/create")
    public Object create(@RequestBody IliquormallLibrary liquorLibrary) {
        return adminLiquorLibraryService.create(liquorLibrary);
    }

    /**
     * 删除酒库
     *
     * @param liquorLibrary
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody IliquormallLibrary liquorLibrary) {
        return adminLiquorLibraryService.delete(liquorLibrary);
    }

    /**
     * 编辑酒库
     *
     * @param liquorLibrary
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:update")
    @PostMapping("/update")
    public Object update(@RequestBody IliquormallLibrary liquorLibrary) {
        return adminLiquorLibraryService.update(liquorLibrary);
    }

    /**
     * 查询酒库
     *
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:read")
    @GetMapping("/list")
    public Object list(String name,
                       String province,
                       String type,
                       String brand,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {
        return adminLiquorLibraryService.list(name, province, type, brand, page, limit);
    }

    /**
     * 酒库详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:read")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        return adminLiquorLibraryService.detail(id);
    }

    /**
     * Excel批量上传
     *
     * @param file
     * @return
     */
    @RequiresPermissions("admin:liquorLibrary:upload")
    @PostMapping("/upload")
    public Object create(@RequestParam("file") MultipartFile file) throws IOException {
        return adminLiquorLibraryService.batchUpload(file.getInputStream());
    }


}
